<template>
  <view>
    <!-- 搜索 -->
    <navigator url="/pages/search/search">
      <uSearch></uSearch>
    </navigator>
    <!-- 轮播 -->
    <u-swiper :list="swiperList" name="image_src" height="340"></u-swiper>
    <!-- 导航 -->
    <view class="nav u-flex">
      <navigator
        v-for="item in navList"
        :key="item.name"
        class="u-flex-1 u-p-15"
      >
        <u-image width="100%" mode="widthFix" :src="item.image_src"></u-image>
      </navigator>
    </view>
    <!-- 楼层 -->
    <view class="floor">
      <!-- 这里使用嵌套循环 -->
      <view
        class="floor-group"
        v-for="item1 in floorList"
        :key="item1.floor_title.name"
      >
        <!-- 楼层标签 -->
        <view class="floor-title">
          <u-image
            width="100%"
            mode="widthFix"
            :src="item1.floor_title.image_src"
          ></u-image>
        </view>
        <!-- 楼层列表 -->
        <view class="floor-list">
          <navigator v-for="item2 in item1.product_list" :key="item2.name">
            <u-image
              width="100%"
              height="100%"
              mode="scaleToFill"
              :src="item2.image_src"
            ></u-image>
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      swiperList: [], //轮播
      navList: [], //导航
      floorList: [], //楼层
    };
  },
  onLoad() {
    this.getSwiperList();
    this.getNavList();
    this.getFloorList();
  },
  methods: {
    // 轮播
    async getSwiperList() {
      // 没有使用封装基础地址时，代码比较多，重复性
      // const [err, res] = await uni.request({
      //   url: "https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata",
      // });
      //  console.log(res);
      // this.swiperList = res.data.message;

      // 使用封装后，代码简单，同时也解决代码的重复性
      this.swiperList = await this.$u.http.get("/home/swiperdata");
    },
    // 导航
    async getNavList() {
      this.navList = await this.$u.http.get("/home/catitems");
    },
    // 楼层
    async getFloorList() {
      this.floorList = await this.$u.http.get("/home/floordata");
    },
  },
};
</script>

<style lang="scss" scoped >
.floor {
  margin-bottom: 10rpx;
}
.floor-title {
  margin-top: 10rpx;
}
.floor-list {
  padding: 0 12rpx;
  overflow: hidden;
  navigator {
    float: left;
  }
  navigator:nth-child(1) {
    width: 232rpx;
    height: 386rpx;
  }
  /* 后四张图片 */
  navigator:nth-last-child(-n + 4) {
    width: 233rpx;
    height: 188rpx;
  }
  navigator:nth-child(2),
  navigator:nth-child(4) {
    margin: 0 15rpx;
  }
  // 后两张图片
  navigator:nth-last-child(-n + 2) {
    margin-top: 10rpx;
  }
}
</style>
